<?php

/**
 * This is the model class for table "optica_bd.Cliente".
 *
 * The followings are the available columns in table 'optica_bd.Cliente':
 * @property integer $idCliente
 * @property string $nombre
 * @property string $apellido
 * @property integer $localidad
 * @property string $direccion
 * @property string $telefono
 * @property string $celular
 * @property string $mail
 * @property string $observacion
 *
 * The followings are the available model relations:
 * @property Localidad $localidad
 * @property Receta[] $recetas
 */
class Cliente extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'optica_bd.cliente';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nombre, apellido, localidad, direccion, celular', 'required'),
			array('localidad', 'numerical', 'integerOnly'=>true),
			array('nombre, apellido', 'length', 'max'=>50),
			array('direccion', 'length', 'max'=>100),
			array('telefono, celular, mail', 'length', 'max'=>45),
			array('observacion', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('idCliente, nombre, apellido, localidad, direccion, telefono, celular, mail, observacion', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'localidad0' => array(self::BELONGS_TO, 'Localidad', 'localidad'),
			'recetas' => array(self::HAS_MANY, 'Receta', 'cliente'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idCliente' => 'Id Cliente',
			'nombre' => 'Nombre',
			'apellido' => 'Apellido',
			'localidad' => 'Localidad',
			'direccion' => 'Direccion',
			'telefono' => 'Telefono',
			'celular' => 'Celular',
			'mail' => 'Mail',
			'observacion' => 'Observacion',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->with = array('localidad0',);
		$criteria->compare('idCliente',$this->idCliente);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('apellido',$this->apellido,true);
		$criteria->compare('localidad0.descripcion',$this->localidad,true);
		$criteria->compare('direccion',$this->direccion,true);
		$criteria->compare('telefono',$this->telefono,true);
		$criteria->compare('celular',$this->celular,true);
		$criteria->compare('mail',$this->mail,true);
		$criteria->compare('observacion',$this->observacion,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
					'defaultOrder'=>'apellido,nombre ASC',
			)
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Cliente the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	public function getFullName(){
		return $this->apellido.', '.$this->nombre;
	}
	function getClients()
	{
		$Clients = Cliente::model()->findAll(array('order'=>'apellido'));
		$list    = CHtml::listData($Clients , 'idCliente', 'fullName');
		return $list;
	}
}
